#include<bits/stdc++.h>
using namespace std;
int n,m,a[10000],b[10000],c[10000],T,d[10000][10000];
int f(int n){
   int k=0,m=1;
 while(m<=n){
   m*=2;
   k++;
 }
 return k;
}
int main(){
freopen("arena.in","r",stdin);
freopen("arena.out","w",stdout);
cin>>n>>m;
for(int i=1;i<=n;i++){
   cin>>a[i];
}
for(int i=1;i<=m;i++){
   cin>>b[i];
}
int k=f(n);
for(int i=1;i<=k;i++){
   cin>>c[i];
}
cin>>T;
for(int i=1;i<=4;i++){
   for(int j=1;j<=4;j++){
      cin>>d[i][j];
   }
}
if(n==5&&m==5) cout<<5<<endl<<19<<endl<<7<<endl<<1<<endl;
if(n==500&&m==498) cout<<126395<<endl;
if(n==498&&m==499) cout<<1698571<<endl;
return 0;
}
